home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip1292.zip / PUSHDIR.C < prev    next >
C/C++ Source or Header  |  1992-12-26  |  5KB  |  186 lines

  1. /*
  2. **  PushDir() and PopDir()
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is an expanded version of the one
  8. **  originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include <dos.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15.  
  16. #define DIR_STACK_SIZE  8
  17. #define MAX_FLEN        67
  18.  
  19. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  20.  
  21. #define BOOL(x) (!(!(x)))
  22.  
  23. /*
  24. **  NOTE: Uses the author's chdrv(), also in SNIPPETS!
  25. */
  26.  
  27. int chdrv(int);
  28.  
  29. static int  PushDir_stack_ptr;
  30. static char PushDir_stack[DIR_STACK_SIZE][MAX_FLEN];
  31.  
  32. /*
  33. **  PushDir()
  34. **
  35. **  Like chdir(), except a drive may be specified and the old directory
  36. **  is saved.
  37. **
  38. **  Arguments: 1 - newdir, the buffer containing the new directory name
  39. **
  40. **  Returns:  -1 - stack overflow
  41. **             0 - error
  42. **             1 - success, still on same drive
  43. **             2 - success, changed drive
  44. **
  45. **  Side effects: Converts name in newdir to upper case and prepends
  46. **                a drive letter.
  47. **
  48. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  49. **           should be at at least MAX_FLEN long.
  50. */
  51.  
  52. int PushDir(char *newdir)
  53. {
  54.       char pname[MAX_FLEN];
  55.       char drive[3];
  56.       char *target = &pname[2];
  57.       int i, new_drv = 0, ercode = 0;
  58.       static int init = 0;
  59.  
  60.       if (!init)
  61.             PushDir_stack_ptr = init = -1;
  62.       if (DIR_STACK_SIZE <= ++PushDir_stack_ptr)
  63.       {
  64.             ercode = -1;
  65.             goto ErrEx;
  66.       }
  67.       getcwd(PushDir_stack[PushDir_stack_ptr], MAX_FLEN);
  68.       strupr(PushDir_stack[PushDir_stack_ptr]);
  69.       strncpy(drive, PushDir_stack[PushDir_stack_ptr], 2);
  70.       drive[2] = '\0';
  71.       if (':' == newdir[1])
  72.       {     /* If a drive is specified                                  */
  73.             strupr(newdir);
  74.             strcpy(pname, newdir);
  75.             if (strchr(target, ':'))      /* if filename is illegal     */
  76.                   goto ErrEx;
  77.             if (*drive != *newdir)
  78.             {
  79.                   if (ERROR == chdrv(newdir[0]))
  80.                   {     /* If the drive is invalid                      */
  81.                         goto ErrEx;
  82.                   }
  83.                   else  new_drv = 1;
  84.             }
  85.       }
  86.       else
  87.       {     /* If a drive isn't specified                               */
  88.             if (!strchr(strupr(newdir), ':'))
  89.             {     /* If legal filename                                  */
  90.                   strcpy(pname, drive);
  91.                   strcat(pname, newdir);
  92.                   strcpy(newdir, pname);
  93.             }
  94.             else
  95.             {     /* If filename is illegal                             */
  96.                   goto ErrEx;
  97.             }
  98.       }
  99.  
  100.       if (*target)
  101.       {
  102.             if (chdir(target))
  103.             {
  104.                   if (1 == new_drv) /* We already changed drives        */
  105.                         chdrv(*drive);    /* Go home before exit        */
  106.                   goto ErrEx;
  107.             }
  108.       }
  109.       return (new_drv + 1);
  110. ErrEx:
  111.       --PushDir_stack_ptr;
  112.       return (ercode);
  113. }
  114.  
  115. /*
  116. **  PopDir()
  117. **
  118. **  Like chdir(), except goes to the drive/directory specified on the
  119. **  top of the PushDir stack.
  120. **
  121. **  Arguments: none
  122. **
  123. **  Returns:  -1 - stack empty
  124. **             0 - error - stack pointer unchanged
  125. **             1 - success, still on same drive
  126. **             2 - success, changed drive
  127. **
  128. **  Side effects: none
  129. **
  130. **  CAUTION: chdir() or chdrv() should not be called between PushDir-
  131. **           PopDir calls.
  132. */
  133.  
  134. int PopDir(void)
  135. {
  136.       char I_am_here[MAX_FLEN], target_drv, *target;
  137.       int new_drv = 0;
  138.  
  139.       if (0 > PushDir_stack_ptr)
  140.             return -1;
  141.       getcwd(I_am_here, MAX_FLEN);
  142.       target = &PushDir_stack[PushDir_stack_ptr][2];
  143.       target_drv = PushDir_stack[PushDir_stack_ptr][0];
  144.       if (I_am_here[0] != target_drv)
  145.       {
  146.             if (ERROR == chdrv(target_drv))
  147.                   return 0;
  148.             new_drv = 1;
  149.       }
  150.       if (!chdir(target))
  151.       {
  152.             --PushDir_stack_ptr;
  153.             return (1 + new_drv);
  154.       }
  155.       else  return 0;
  156. }
  157.  
  158. /*
  159. **  isdir()
  160. **
  161. **  Checks to see if a drive and/or path are a valid directory.
  162. **
  163. **  Arguments: 1 - dir, the buffer containing the new directory name
  164. **
  165. **  Returns: ERROR - push/popdir stack overflow
  166. **           FALSE - not a valid directory
  167. **           TRUE  - valid directory
  168. **
  169. **  Side effects: Converts name in dir to upper case and prepends a
  170. **                drive letter.
  171. **
  172. **  CAUTION: Since a drive will be prepended to newdir, it's buffer
  173. **           should be at at least MAX_FLEN long.
  174. */
  175.  
  176. int isdir(char *dir)
  177. {
  178.       int ercode;
  179.  
  180.       if (-1 == (ercode = pushdir(dir)))
  181.             return ercode;
  182.       if (ercode)
  183.             popdir();
  184.       return BOOL(ercode);
  185. }
  186.